package com.ocean.dynamic;

/**
 * <Description> <br>
 *
 * @author linmiaolai@sanyygp.com<br>
 * @version 1.0<br>
 * @date 2023/03/31 <br>
 */
public class Dp01_2 {
    public static void main(String[] args) {
        int[] weight = {1, 3, 4};
        int[] value = {15, 20, 30};
        int bagSize = 4;
        Dp01_2 dp01 = new Dp01_2();
        dp01.testWeightBagProblem(weight, value, bagSize);
    }

    private void testWeightBagProblem(int[] weight, int[] value, int bagSize) {
        // dp[i]  背包容量为i时 背包的最大价值
        int[] dp = new int[bagSize + 1];
        // 初始化
        int length = weight.length;
        for (int i = 0; i < length; i++) {
            for (int j = bagSize; j >= weight[i]; j--){
                dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
            }
        }
        //打印dp数组
        for (int j = 0; j <= bagSize; j++){
            System.out.print(dp[j] + " ");
        }
    }
}
